package com.ruoyi;


import com.alibaba.fastjson.JSONObject;
import com.ruoyi.system.service.MatchingService;
import com.ruoyi.system.domain.SysContracts;
import com.ruoyi.system.service.impl.MatchingServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Base64;

@RestController
public class TPCService{
    @Autowired
    MatchingService matchingService;

    @PostConstruct
    public void tcpService() {
        try {
            // 创建TCP服务器并绑定到指定端口
            int port = 8888;
            ServerSocket serverSocket = new ServerSocket(port);
            System.out.println("服务器已启动，等待客户端连接...");
            while (true){
                // 等待客户端连接
                Socket clientSocket = serverSocket.accept();
                System.out.println("客户端连接成功");
                // 创建输入流和输出流
                InputStream inputStream = clientSocket.getInputStream();
                OutputStream outputStream = clientSocket.getOutputStream();
                // 读取客户端发送的数据
                byte[] buffer = new byte[1024];
                int bytesRead = inputStream.read(buffer);
                String receivedMessage = new String(buffer, 0, bytesRead);
                String key = "1234567890123456"; // 16字节密钥
                // 解密---添加时序数据库(未实现)
                String[] split = receivedMessage.split("\\*xy\\*");
                if (split!=null){
                    for (String s : split) {
                        System.out.println(s);
                        String decryptedData = decrypt(s, key);
                        SysContracts object = JSONObject.parseObject(decryptedData, SysContracts.class);
                        System.out.println("收到消息: " + object);
//                        forZSet.add("matchings:"+object.getScokName()+"-"+object.getFalg(),object,object.getScokPrice().doubleValue());
                        String keys="matchings:"+object.getScokName()+"-"+((object.getFalg()+1)%2);
                        System.err.println(keys);
                        matchingService.matching(object,keys);
                    }
                }
                // 发送响应数据给客户端
                String responseMessage = "Hello, Client!";
                outputStream.write(responseMessage.getBytes());
                // 关闭连接
//                clientSocket.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    private static String decrypt(String encryptedData, String key) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
        return new String(decryptedBytes);
    }
}
